﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using PBOffice.DAL;

namespace PBOffice.BLL
{
   public class NewsManager
    {
       //未实现：根据新闻标题和新闻类型进行模糊查询新闻信息

        #region 查询新闻类型信息
        /// <summary>
        /// 查询新闻类型信息
        /// </summary>
        /// <returns></returns>
        public List<NewsType> ShowNewsType()
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var newsType = from n in myOffice.NewsTypes
                               select n;         //查询新闻类型信息
                return newsType.ToList();
            }
        } 
        #endregion

        #region 查询所有新闻信息
        /// <summary>
        /// 查询所有新闻信息
        /// </summary>
        /// <param name="newsTypeNo"></param>
        /// <returns></returns>
        public List<v_NewsInfo> ShowAllNewsInfo()
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news = from n in myOffice.v_NewsInfo
                           orderby n.PublishDate descending     //按时间倒叙排列
                           select n;         //根据新闻类型查询新闻信息
                return news.ToList();
            }
        }
        #endregion

        #region 查询新闻信息（根据新闻类型）
        /// <summary>
        /// 查询新闻信息（根据新闻类型）
        /// </summary>
        /// <param name="newsTypeNo"></param>
        /// <returns></returns>
        public List<v_NewsInfo> ShowNewsInfo(int newsTypeNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news = from n in myOffice.v_NewsInfo
                           orderby n.PublishDate descending     //按时间倒叙排列
                           where n.NewsTypeNo == newsTypeNo
                           select n;         //根据新闻类型查询新闻信息
                return news.ToList();
            }
        }  
        #endregion

        #region 查询新闻信息（根据新闻标题）
        /// <summary>
        /// 查询新闻信息（根据新闻标题）
        /// </summary>
        /// <param name="newsTypeNo"></param>
        /// <returns></returns>
        public List<v_NewsInfo> ShowNewsInfoByNewsTitle(string newsTitle)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news = from n in myOffice.v_NewsInfo
                           orderby n.PublishDate descending     //按时间倒叙排列
                           where n.NewsTitle.Contains(newsTitle)
                           select n;         //根据新闻类型查询新闻信息
                return news.ToList();
            }
        }
        #endregion

        #region 查询新闻信息（根据新闻标题和新闻类型）
        /// <summary>
        /// 查询新闻信息（根据新闻标题和新闻类型）
        /// </summary>
        /// <param name="newsTypeNo"></param>
        /// <returns></returns>
        public List<v_NewsInfo> ShowNewsInfoByNewsTypeAndNewsTitle(int newsTypeNo,string newsTitle)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news = from n in myOffice.v_NewsInfo
                           orderby n.PublishDate descending     //按时间倒叙排列
                           where n.NewsTypeNo == newsTypeNo && n.NewsTitle.Contains(newsTitle)
                           select n;         //根据新闻类型查询新闻信息
                return news.ToList();
            }
        }
        #endregion

        #region 查询新闻信息（根据新闻编号）
        /// <summary>
        /// 查询新闻信息（根据新闻编号）
        /// </summary>
        /// <param name="newsTypeNo"></param>
        /// <returns></returns>
        public News ShowNewsInfoByNewsNo(int newsNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news = from n in myOffice.v_NewsInfo
                           where n.NewsNo==newsNo
                           select n;         //根据新闻类型查询新闻信息
                return (News) news;
            }
        }
        #endregion

        #region 添加新闻信息
        /// <summary>
        /// 添加新闻信息
        /// </summary>
        /// <param name="n">新闻对象</param>
        /// <returns></returns>
        public bool AddNewsInfo(News n,int userNo,int newsTypeNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news = myOffice.News.CreateObject();              
                    news.NewsTitle = n.NewsTitle;//新闻标题
                    news. NewsContent = n.NewsContent;//新闻内容
                    news. IsRecommend = n.IsRecommend;//是否推荐
                    news. IsTop = n.IsTop;//是否置顶
                    news. ClickNum = n.ClickNum;//浏览人数
                    news.PublishDate = n.PublishDate;//发布时间    
                    news.User = myOffice.Users.SingleOrDefault(user => user.UserNo==userNo);  //发布用户
                    news.NewsType = myOffice.NewsTypes.SingleOrDefault(newsType => newsType.NewsTypeNo==newsTypeNo );//新闻编号
                myOffice.News.AddObject(news);     //添加新闻信息到数据库
                return myOffice.SaveChanges() > 0;  //添加成功，返回true；否则，返回false
            }
        } 
        #endregion

        #region 修改新闻信息（根据新闻编号）
        /// <summary>
        /// 修改新闻信息（根据新闻编号）
        /// </summary>
        /// <param name="n">新闻对象</param>
        /// <returns></returns>
        public bool ChangeNewsInfo(News n)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news1 = from news2 in myOffice.News
                            where news2.NewsNo == n.NewsNo
                            select news2;
                news1.First();
                News news = (News)news1;
                news.NewsTitle = n.NewsTitle; //新闻标题
                news.NewsType.NewsTypeNo = n.NewsType.NewsTypeNo;   //新闻类型
                news.NewsContent = n.NewsContent;  //新闻内容
                news.PublishDate = n.PublishDate;  //发布时间
                news.IsRecommend = n.IsRecommend;  //是否推荐
                news.IsTop = n.IsTop;              //是否置顶
                news.ClickNum = n.ClickNum;        //浏览人数
                news.User.UserNo = news.User.UserNo;  //发布人
                return myOffice.SaveChanges() > 0;      //保存修改后的新闻信息，修改成功，返回true，否则，返回false
            }
        } 
        #endregion

        #region 删除新闻信息（根据新闻编号）
        /// <summary>
        /// 删除新闻信息（根据新闻编号）
        /// </summary>
        /// <param name="newsNo"></param>
        /// <returns></returns>
        public bool DeleteNewsInfo(int newsNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var news1 = from news2 in myOffice.News
                            where news2.NewsNo == newsNo
                            select news2;
                news1.First();
                News news = (News)news1;
                myOffice.News.DeleteObject(news);   //删除新闻信息
                return myOffice.SaveChanges() > 0;      //保存删除新闻后数据库的信息，删除成功，返回true；否则，返回false
            }
        } 
        #endregion

    }
}
